package com.tkjt.pms.modules.indexcalculator.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tkjt.pms.calc.KpiCalculator;
import com.tkjt.pms.common.entity.TkIndexCalculator;
import com.tkjt.pms.common.entity.TkIndexValueAudit;
import com.tkjt.pms.modules.common.service.TkIndexValueAuditService;
import com.tkjt.pms.modules.indexcalculator.service.TkIndexCalculatorService;
import com.tkjt.pms.modules.indexcalculator.thread.CaculateThread;
import io.swagger.annotations.Api;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;

@Api(value="TkIndexCalculator",description="tk_index_calculator",tags="/api/tkIndexCalculatorController")
@RestController
@RequestMapping("/api/tkIndexCalculatorController")
public class TkIndexCalculatorController {
    @Autowired
    private TkIndexCalculatorService tkIndexCalculatorService;
    @Autowired
    private TkIndexValueAuditService tkIndexValueAuditService;
    @Autowired
    private KpiCalculator kpiCalculator;

    /**
     * 绩效指标计算状态列表查询
     * @param tkIndexCalculator
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @RequestMapping(value = "datagrid", method = RequestMethod.POST)
    public Result<?> datagrid(@RequestBody TkIndexCalculator tkIndexCalculator, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<TkIndexCalculator> queryWrapper = QueryGenerator.initQueryWrapper(tkIndexCalculator, req.getParameterMap());
        Page<TkIndexCalculator> page = new Page<TkIndexCalculator>(pageNo, pageSize);
        try {
            IPage<TkIndexCalculator> pageList = tkIndexCalculatorService.page(page, queryWrapper);
            return Result.ok(pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }

    }


    /**
     * 计算公式
     *
     * @return
     */
    @RequestMapping(value = "caculate", method = RequestMethod.POST)
    public Result<?> caculate(@RequestBody TkIndexCalculator tkIndexCalculator, HttpServletRequest request) {
        String message = null;
        //存入计算中信息
        TkIndexCalculator t = tkIndexCalculatorService.getById(tkIndexCalculator.getId());
        if(t != null){
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH );
            if(month == 0){
                month = 12;
                year = year -1;
            }
            String indexGroup = t.getIndexGroup();
            QueryWrapper<TkIndexValueAudit> auditQueryWrapper = new QueryWrapper<>();
            auditQueryWrapper.eq("INDEX_VALUE_Y",year);
            auditQueryWrapper.eq("INDEX_VALUE_M",month);
            if("0".equals(indexGroup)){
                indexGroup = "A01A01A01";
                auditQueryWrapper.eq("INDEX_GROUP",indexGroup);
                TkIndexValueAudit indexValueAuditEntity1 = tkIndexValueAuditService.getOne(auditQueryWrapper);
                if (indexValueAuditEntity1 != null) {
                    if("2".equals(indexValueAuditEntity1.getAuditStatus())){
                        return Result.error("部门指标正在审核中，不允许修改");
                    }else if("3".equals(indexValueAuditEntity1.getAuditStatus())){
                        return Result.error("部门指标已审核，不允许修改");
                    }

                }
                indexGroup = "A01A01A02";
                auditQueryWrapper.eq("INDEX_GROUP",indexGroup);
                TkIndexValueAudit indexValueAuditEntity2 = tkIndexValueAuditService.getOne(auditQueryWrapper);
                if (indexValueAuditEntity2 != null) {
                    if("2".equals(indexValueAuditEntity2.getAuditStatus())){
                        return Result.error("部门指标正在审核中，不允许修改");
                    }else if("3".equals(indexValueAuditEntity2.getAuditStatus())){
                        return Result.error("部门指标已审核，不允许修改");
                    }

                }
            }else if("1".equals(indexGroup)){
                indexGroup = "A01A01A02";
                auditQueryWrapper.eq("INDEX_GROUP",indexGroup);
                TkIndexValueAudit indexValueAuditEntity1 = tkIndexValueAuditService.getOne(auditQueryWrapper);
                if (indexValueAuditEntity1 != null) {
                    if("2".equals(indexValueAuditEntity1.getAuditStatus())){
                        return Result.error("部门指标正在审核中，不允许修改");
                    }else if("3".equals(indexValueAuditEntity1.getAuditStatus())){
                        return Result.error("部门指标已审核，不允许修改");
                    }

                }
            }else{
                indexGroup = "A01A01A01";
                auditQueryWrapper.eq("INDEX_GROUP",indexGroup);
                TkIndexValueAudit indexValueAuditEntity1 = tkIndexValueAuditService.getOne(auditQueryWrapper);
                if (indexValueAuditEntity1 != null) {
                    if("2".equals(indexValueAuditEntity1.getAuditStatus())){
                        return Result.error("部门指标正在审核中，不允许修改");
                    }else if("3".equals(indexValueAuditEntity1.getAuditStatus())){
                        return Result.error("部门指标已审核，不允许修改");
                    }

                }
            }


        }
        t.setCalcuStatus(2);
        t.setCalcuDate(new Date());
        t.setUpdateDate(new Date());
        //todo 当前用户真实姓名
        //t.setUpdateBy(((LoginUser) SecurityUtils.getSubject().getPrincipal()).getRealname());
        try {
            tkIndexCalculatorService.saveOrUpdate(t);
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        try {
            new Thread(new CaculateThread(tkIndexCalculator,tkIndexCalculatorService,kpiCalculator)).start();
        } catch (Exception e) {
            //存入计算异常信息
            TkIndexCalculator t1 =tkIndexCalculatorService.getById(tkIndexCalculator.getId());
            t1.setCalcuStatus(4);
            t1.setCalcuDate(new Date());
            t1.setUpdateDate(new Date());
            //todo 当前用户真实姓名
            //t1.setUpdateBy(((LoginUser) SecurityUtils.getSubject().getPrincipal()).getRealname());
            t1.setCalcuLogs(e.getMessage());
            try {
                tkIndexCalculatorService.saveOrUpdate(t1);
            } catch (Exception e1) {
                e1.printStackTrace();
            }

            e.printStackTrace();
        }
        return Result.ok();
    }
}
